import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'


Vue.use(Router)



var router =  new Router({
  routes: [
    {
      path: '/Login',
      name: 'Login',
      component: Login
    },
    {
      path:'/',
      name: 'Home',
      component:Home
    }
  ]
})

//配置所有的路由规则
//1.导入组件.2.书写路由规则
//注册一个全局守卫,作用是在路由跳转前,对路由进行判断,防止没有登录的用户跳转到登录页面
router.beforeEach((to, from, next)=>{
  //获取mytoken,如果不希望拦截就调用next()
  let token = localStorage.getItem('mytoken')
  if(token){
    // 如果已经登录,则不拦截
    next()
  }else{
    //如果没有登录,去往的不是登录页面则拦截,反而不拦截
    if(to.path !== '/Login'){
     next({path:'/Login'})
    }else{
      next()
    }
  }
})

export default router